package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {



    @Autowired
   private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
        //数据库中新增一个菜品后 要保持与redis中的一致 要删除原先的缓存 然后再添加

        //构造一个redis中的key
        String key = "dish_" + dishDTO.getCategoryId();
        //删除操作
        redisTemplate.delete(key);

        return Result.success();

    }


    /**
     * 菜品分页查询功能
     * @param dishPageQueryDTOn
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTOn){
        log.info("菜品分页查询：{}",dishPageQueryDTOn);
       PageResult pageResult =  dishService.pageQuery(dishPageQueryDTOn);
       return Result.success(pageResult);
    }



    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除：{}",ids);
        dishService.deleteBatch(ids);

        cleanCache("*dish_*");

        return Result.success();
    }


    /**
     * 菜品信息修改
     * 先根据传入id查询回显数据（当点击某个菜品的修改键时调用
     * 点击保存时调用修改功能
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        //编辑框回显的信息
       log.info("根据id查询菜品信息：{}",id);
        DishVO dishVO = dishService.getDishWithFlavorById(id);
        return Result.success(dishVO);
    }
    //当点击保存时 修改和新增口味
    @PutMapping()
    public Result updatedishAndAddFlavor(@RequestBody DishDTO dishDTO){
        log.info("修改编辑框中的信息：{}",dishDTO);
        //修改编辑框回显的信息
        dishService.updatedishAndAddFlavor(dishDTO);

        cleanCache("*dish_*");

        return Result.success();
    }


    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        dishService.startOrStop(status,id);
        // 获取所有以"dish_"开头的key集合
        cleanCache("*dish_*");

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        log.info("开始根据分类id查询菜品{}",categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    private void cleanCache(String pattern){
        // 获取所有以"dish_"开头的key集合
        Set keys = redisTemplate.keys(pattern);
        //删除操作
        redisTemplate.delete(keys);
    }
}
